package com.clhbolg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clhbolg.domain.ExceptionLog;
import com.clhbolg.domain.bo.ExceptionLogBo;
import com.clhbolg.domain.vo.ExceptionLogVo;
import com.clhbolg.mapper.ExceptionLogMapper;
import com.clhbolg.service.IExceptionLogService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author cailinhu
 * @date 2023-02-08
 */
@RequiredArgsConstructor
@Service
@DS("slave")
public class ExceptionLogServiceImpl implements IExceptionLogService {

    private final ExceptionLogMapper baseMapper;

    /**
     * 查询【请填写功能名称】
     */
    @Override
    public ExceptionLogVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public TableDataInfo<ExceptionLogVo> queryPageList(ExceptionLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ExceptionLog> lqw = buildQueryWrapper(bo);
        Page<ExceptionLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @Override
    public List<ExceptionLogVo> queryList(ExceptionLogBo bo) {
        LambdaQueryWrapper<ExceptionLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ExceptionLog> buildQueryWrapper(ExceptionLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExceptionLog> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getOptUri()), ExceptionLog::getOptUri, bo.getOptUri());
        lqw.eq(StringUtils.isNotBlank(bo.getOptMethod()), ExceptionLog::getOptMethod, bo.getOptMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestMethod()), ExceptionLog::getRequestMethod, bo.getRequestMethod());
        lqw.eq(StringUtils.isNotBlank(bo.getRequestParam()), ExceptionLog::getRequestParam, bo.getRequestParam());
        lqw.eq(StringUtils.isNotBlank(bo.getOptDesc()), ExceptionLog::getOptDesc, bo.getOptDesc());
        lqw.eq(StringUtils.isNotBlank(bo.getExceptionInfo()), ExceptionLog::getExceptionInfo, bo.getExceptionInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getIpAddress()), ExceptionLog::getIpAddress, bo.getIpAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getIpSource()), ExceptionLog::getIpSource, bo.getIpSource());
        return lqw;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public Boolean insertByBo(ExceptionLogBo bo) {
        ExceptionLog add = BeanUtil.toBean(bo, ExceptionLog.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改【请填写功能名称】
     */
    @Override
    public Boolean updateByBo(ExceptionLogBo bo) {
        ExceptionLog update = BeanUtil.toBean(bo, ExceptionLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ExceptionLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除【请填写功能名称】
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
